<template>
  <div class="container">
    <div class="content def">
      <div class="categories">
        <p style="font-size:24px;">类别：<span style="color:#c69c6c;">{{type}}</span></p>
      </div>
    </div>

    <div class="content">
        <!-- 数据未回来之前的假dom -->
      <div class="item" v-show="goods.length === 0" v-for="(skeleton,index) in skeletons" :key="index">
        <div style="height:224px;overflow:hidden;">
          <img :src="skeleton.url" :alt="skeleton.title">
        </div>
        <div class="text">
          <p style="color:gray;margin:10px 3px 10px 3px;"></p>
        </div>
        <div class="operation">
          <span style="color:#c69c6c;">{{skeleton.title}}</span>
        </div>
      </div>
      <!-- 真实dom -->
    <div @click="goDetail(item)" class="item" v-show="goods.length !== 0" 
        v-for="item in goods" :key="item.goods_id">
    <div style="height:224px;overflow:hidden;">
        <img v-lazy="item.goods_url" >
    </div>
    <div class="text">
        <p style="color:gray;margin:10px 3px 10px 3px;">{{item.goods_title}}</p>
    </div>
    <div class="operation">
        <span style="color:#e5505a;">￥{{item.goods_price}}</span>
        <button @click.stop="addCar(item.goods_id)">购物车</button>
        <button @click.stop="purchase(item)" style="background:#e5505a;">购买</button>
    </div>
    </div>
        <div style="clear:both;"></div>
    </div>
  </div>
</template>

<script>
import {reqTypeGoods,reqAddCar} from '../api'
export default {
    name:'Type',
    data() {
        return {
            goods:[],
            skeletons:[ //用于数据未请求来时渲染一个假dom，撑起屏幕
                {url:require('../assets/lzay.gif'),title:'正在加载。。。'}
            ],
        }
    },
    props:['type'],
    methods: {
      addCar(id){ //添加到购物车
        if (!this.$local.isExist('key')) {
          this.$message.error('登录令牌不存在！')
          this.$router.push('/user/info')
        }else{
          if(reqAddCar(id,this.$local.get('key')).exist === false) {
            this.$message.error('登录令牌失效，请重新登录！')
            this.$router.push('/user/info')
          }else{
            this.$message.success('添加到购物车成功！')
          }
        }
      },
      purchase(item){ //购买
        if (this.$local.isExist('key')) {
          item.car_goods_num='1'
          let arr = []
          arr.push(item)
          const array = JSON.stringify(arr)
          this.$router.push({path:'/settlement',query:{value:array}})
        }else{
          this.$message.error('登录令牌不存在！')
          this.$router.push('/user/info')
        }
      },
      goDetail(item){
        const array = JSON.stringify(item)
        this.$router.push({path:'/detail',query:{value:array}})
      }
    },
    async mounted() {
        this.goods = await reqTypeGoods(this.type)
    },
}
</script>

<style scoped>
    .def{
        margin-top: 30px;
        margin-bottom: 20px;
    }
    .categories{
        color: black;
        background: white;
        border-radius: 8px;
        padding: 10px 0 10px 20px;
        user-select: none;
    }
    .item{
    width: 236px;
    float: left;
    border-radius: 8px;
    margin-right: 20px;
    overflow: hidden;
    /* background: #25252B; */
    background: #fff;
    transform: translateY(0);
    /* 移回也需要0.4s时间过渡 */
    transition: transform 0.2s linear 0s;
    transition: all 0.4s;
  }
  .item:hover{
    box-shadow: 0 0 15px 0 gray;
    transform: translateY(-5px);
    /* 0.4s完成transform移动效果*/
    transition: transform 0.2s linear 0s;
    transition: all 0.4s;
  }
  .item:nth-of-type(4n+5){
    margin-right: 0;
  }
  .item:nth-of-type(n+6){
    margin-top: 20px;
  }
  .operation{
    margin: 3px 0 5px 5px;
  }
  .operation button{
    float: right;
    margin-right: 5px;
    margin-bottom: 5px;
  }
  .text{
    /* color: rgba(255, 255, 255, 0.75); */
    font-size: 14px;
    max-width: 100%;
    height: 40px;
    padding:0 5px;
  }
  .text p{
    font-size: 12px;
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
</style>